//岛屿的最大面积
//测试链接：https://leetcode.cn/problems/max-area-of-island/
public class MaxAreaOfIsland {
    int[] dx = {-1, 1, 0, 0};
    int[] dy = {0, 0, -1, 1};
    boolean[][] vis;
    int max, n, m;
    public int maxAreaOfIsland(int[][] grid) {
        n = grid.length; m = grid[0].length; max = 0;
        vis = new boolean[n][m];

        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                if(!vis[i][j] && grid[i][j] == 1) {
                    int temp = dfs(grid,i,j,1);
                    if(temp > max) {
                        max = temp;
                    }
                }
            }
        }
        return max;
    }

    public int dfs(int[][] grid, int i , int j, int area) {
        vis[i][j] = true;
        for(int k = 0; k < 4; k++) {
            int x = dx[k] + i;
            int y = dy[k] + j;
            if(x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && grid[x][y] == 1) {
                area = dfs(grid, x, y, area+1);
            }
        }
        return area;
    }
}
